<?php

namespace addons\mall\controller;


/**
 * 用户地址
 *
 * @icon fa fa-circle-o
 */
class Invoice extends Base
{
    /**
     * Address模型对象
     * @var \app\common\model\mall\user\Address
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\order\Invoice;

    }

    /**
     * 查看
     */
    public function index(){
        $data = $this->model->where(['user_id'=>$this->auth->id,'status'=>"normal"])->select();
        $this->assign('invoiceId',(int)input('invoiceId'));
        $this->assign('isInvoice',(int)input('isInvoice'));
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                try
                {
                    $validate = '\app\common\validate\mall\order\Invoice.add';
                    $result = $this->validate($params,$validate);
                    if(true !== $result){
                        // 验证失败 输出错误信息
                        $this->error($result);
                    }
                    $params['user_id'] = $this->auth->id;
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false)
                    {
                        $this->success();
                    }
                    else
                    {
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->fetch();
    }

    /**
     * 编辑
     */
    public function edit()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                try
                {
                    $validate = '\app\common\validate\mall\order\Invoice.edit';
                    $result = $this->validate($params,$validate);
                    if(true !== $result){
                        // 验证失败 输出错误信息
                        $this->error($result);
                    }

                    $result = $this->model->allowField(true)->save($params,['id'=>$params['id']]);
                    if ($result !== false)
                    {
                        $this->success();
                    }
                    else
                    {
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if($this->request->param("table")) $this->model = model($this->request->param("table"));
        if ($ids)
        {
            $pk = $this->model->getPk();
            $list = $this->model->where($pk, 'in', $ids)->select();
            $count = 0;
            foreach ($list as $k => $v)
            {
                $count += $v->delete();
            }
            if ($count)
            {
                $this->success();
            }
            else
            {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    /**
     * 批量更新
     */
    public function multi($ids = "")
    {
        if($this->request->param("table")) $this->model = model($this->request->param("table"));

        $ids = $ids ? $ids : $this->request->param("ids");
        if ($ids)
        {
            if ($this->request->has('params'))
            {
                parse_str($this->request->post("params"), $values);
                $values = array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
                if ($values)
                {
                    $user_ids = $this->getDataLimitUserIds();
                    if (is_array($user_ids))
                    {
                        $where[]=[$this->dataLimitField, 'in', $user_ids];
                    }
                    $where[]=[$this->model->getPk(), 'in', $ids];
                    $count = $this->model->where($where)->update($values);
                    if ($count)
                    {
                        $this->success();
                    }
                    else
                    {
                        $this->error(__('No rows were updated'));
                    }
                }
                else
                {
                    $this->error(__('You have no permission'));
                }
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
}
